<?php
/**
 * Created by PhpStorm.
 * User: ZL
 * Date: 2016/4/19
 * Time: 10:54
 */
namespace Api\Controller;
use Think\Controller;
use ZL\Curl;

class TestController extends Controller
{
    public function test1($id){
        $res = repairSend($id);
        var_dump($res);
        $html = json_decode($res);
        var_dump($html);die;
    }
    public function test() {
        $data = array();
        $data['room_id'] = 15359;
        $data['customer_id'] = 20;
//        $url = "http://zhcrm.wx91go.com/index.php?m=Api&c=data&a=accountPay";
        $url = "http://zhcrm.wx91go.com/index.php?m=Api&c=data&a=customerAuthentication";
        $html = apiPost($url, $data);
        var_dump($html);
        $html = json_decode($html);
        var_dump($html);die;
        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                echo ' - No errors';
                break;
            case JSON_ERROR_DEPTH:
                echo ' - Maximum stack depth exceeded';
                break;
            case JSON_ERROR_STATE_MISMATCH:
                echo ' - Underflow or the modes mismatch';
                break;
            case JSON_ERROR_CTRL_CHAR:
                echo ' - Unexpected control character found';
                break;
            case JSON_ERROR_SYNTAX:
                echo ' - Syntax error, malformed JSON';
                break;
            case JSON_ERROR_UTF8:
                echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            default:
                echo ' - Unknown error';
                break;
        }
            
        print '<pre>';
        print_r($data);
    }

    public function postTest()
    {
        $data['id'] = 30;
        $data['pay_time'] = 1313141423;
        $data['payer_name'] = 'sasa';
        $data['pay_money'] = '1';
        $url = 'http://zhcrm.wx91go.com/index.php?m=Api&c=Data&a=accountPay';
        var_dump($data);
        $dd = apiPost($url, $data);
        echo $dd;
    }
    //前台客服数据展示
    public function getAssistant(){
        $data = M('User')->alias('a')->field('a.user_name,a.real_name,a.DEPT_NAME,c.name')->join("left join zh_role_user b on a.id=b.user_id
        left join zh_community c on a.assistant_map=c.id")->where(array('b.role_id'=>4))->select();
        foreach($data as &$v){
            echo '账号是:'.$v['user_name'];
            echo '       真名是:'.$v['real_name'];
            echo '       密码是:123456';
            if(!$v['DEPT_NAME'])
                $v['DEPT_NAME'] = $v['name'];
            echo '       小区是:'.$v['DEPT_NAME'];
            echo '<br/>';
        }
//        echo '<pre>';
//        print_r($data);die;
    }
    //有个房间和单元错了 删了重新生成
    public function roomEdit(){
        die;
        //删除单元和房间
        M('Unit')->where(array('zone_id'=>86))->delete();
        M('Room')->where(array('zone_id'=>86))->delete();
        M('Building')->data(array('children_count'=>0))->where(array('zone_id'=>86))->save();
        //
        $builData = M('Building')->where(array('zone_id'=>86))->select();
//        echo '<pre>';
//        var_dump($builData);die;
        foreach ($builData as $v){
            $data = array();
            $data['no'] = $v['no'].sprintf('%01d',($v['children_count']+1));
            $data['status'] = 1;
            $data['remark'] = '';
            $data['children_count'] = 0;
            $data['building_id'] = $v['id'];
            $building = D('Building')->find($data['building_id']);
            $data['full_parent_name'] = $building['full_parent_name'].$building['name'];
            $data['community_id'] = $building['community_id'];
            $data['zone_id'] = $building['zone_id'];

            $unit['id'] = D('Admin/Unit')->add($data);
            D('Admin/Building')->where(array('id'=>$v['id']))->setInc('children_count',1);
            $unit = M('Unit')->find($unit['id']);
            echo '<pre>';
            var_dump($unit);
            echo '<br/>';

            for ($x=1; $x<=7; $x++) {
                $data = array();
                $data['no'] = $unit['no'].sprintf('%04d',($unit['children_count']+1));
                $data['name'] = $v['name'].'-'.$x;
                $data['unit_id'] = $unit['id'];
                $unit = D('Admin/Unit')->find($data['unit_id']);
                $data['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
                $data['community_id'] = $unit['community_id'];
                $data['zone_id'] = $unit['zone_id'];
                $data['building_id'] = $unit['building_id'];
                $roomModel = D('Admin/Room');
                $room = $roomModel->create($data);
                $room['id']= $roomModel->add($room);


                echo '<pre>';
                var_dump($room);
                echo '<br/>';
                D('Unit')->where(array('id'=>$unit['id']))->setInc('children_count',1);
            }
        }
    }

    //重置小区后面的名字 full_parent_name    老方法不用了
    public function reNameOld(){
        $i = I('i');

        $roomData = M('Room')->page($i, 1000)->order('id desc')->select();
//        echo '<pre>';
//        var_dump($roomData);die;
        foreach($roomData as $v){
//            //修改zone
//            $community = M('Community')->find($v['community_id']);
//            $zone = array();
//            $zone['full_parent_name'] = $community['full_parent_name'].$community['name'];
//            M('Zone')->data($zone)->where(array('id'=>$v['zone_id']))->save();
//            //修改building
//            $building = array();
//            $zone = M('Zone')->find($v['zone_id']);
//            $building['full_parent_name'] = $zone['full_parent_name'].$zone['name'];
//            M('Building')->data($building)->where(array('id'=>$v['building_id']))->save();
//            //修改unit
//            $unit = array();
//            $building = M('Building')->find($v['building_id']);
//            $unit['full_parent_name'] = $building['full_parent_name'].$building['name'];
//            M('Unit')->data($unit)->where(array('id'=>$v['unit_id']))->save();
//            //修改room
//            $room = array();
//            $unit = M('Unit')->find($v['unit_id']);
//            $room['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
//            M('Room')->data($room)->where(array('id'=>$v['id']))->save();
            //修改家庭
            $famliy = array();
            $room = M('Room')->find($v['id']);
            $famliy['full_parent_name'] = $room['full_parent_name'].$room['name'];
            $Family = M('Family')->data($famliy)->where(array('room_id'=>$v['id']))->save();
        }
    }

    //重置小区后面的名字 full_parent_name
    public function reName($clear=false){
//        $i = I('i');
        if($clear) {
            S('reName', null);
            return '清除完毕';
        }
        $cd = M('Community')->select();
        foreach ($cd as $cid) {
            $check = '-'.$cid['id'].'-';
            if(strpos(S('reName'),$check))
                continue;
            $where['community_id'] = $cid['id'];
            $roomData = M('Room')->where($where)->select();
//        echo '<pre>';
//        var_dump($roomData);die;
            foreach ($roomData as $v) {
                //修改zone
                $community = M('Community')->find($v['community_id']);
                $zone = array();
                $zone['full_parent_name'] = $community['full_parent_name'] . $community['name'];
                M('Zone')->data($zone)->where(array('id' => $v['zone_id']))->save();
                //修改building
                $building = array();
                $zone = M('Zone')->find($v['zone_id']);
                $building['full_parent_name'] = $zone['full_parent_name'] . $zone['name'];
                M('Building')->data($building)->where(array('id' => $v['building_id']))->save();
                //修改unit
                $unit = array();
                $building = M('Building')->find($v['building_id']);
                $unit['full_parent_name'] = $building['full_parent_name'] . $building['name'];
                M('Unit')->data($unit)->where(array('id' => $v['unit_id']))->save();
                //修改room
                $room = array();
                $unit = M('Unit')->find($v['unit_id']);
                $room['full_parent_name'] = $unit['full_parent_name'] . $unit['name'];
                M('Room')->data($room)->where(array('id' => $v['id']))->save();
                //修改家庭
                $famliy = array();
                $room = M('Room')->find($v['id']);
                $famliy['full_parent_name'] = $room['full_parent_name'] . $room['name'];
                $Family = M('Family')->data($famliy)->where(array('room_id' => $v['id']))->save();
            }
            echo '<br/>';
            echo $cid['id'];
            $now = '-'.$cid['id'].'-'.S('reName');
            S('reName', $now);
        }
    }

    public function testApi(){
        $url = 'http://smart.wx91go.com/index.php?g=Home&m=Index&a=HouseCharge';
        $data = array();
        apiPost($url, $data);
    }

    public function reNo(){
        $i = I('i');
        echo $i;
        echo '<br/>';
        $i = $i*1000;
        $data = M('Room')->limit($i,1000)->select();
//        myDump($data);
        foreach ($data as $k=>&$v) {
            echo $v['id'];
            echo '<br/>';
            $v['no'] = substr($v['no'],-4);
            D('Admin/Room')->data($v)->save();
        }
    }
//无用方法
    public function ddcus(){
        $wxcsData = M('QtwyCustomer','tp_','DB_WX')->select();
        echo count($wxcsData);
        foreach ($wxcsData as $v){
            if($v['crm_id']){
                $data = array();
                $data = M('Customer')->where(array('id'=>$v['crm_id']))->where('attestation_tel is null')->find();
                if($data) {
                    $data['attestation_tel'] = $data['tel'];
                    $data['attestation_tel'] = $data['tel'];
                    M('Customer')->data($data)->save();
                    $dd[] = $data;
                }
            }
        }
        echo '<br/>';
        echo count($dd);
    }


    public function notPay(){
        $data = D('Assistant/Expense')->notPay();
        myDump($data);
    }

    public function nameBack(){
        $users = M('User')->select();
        foreach ($users as &$v) {
            $v['user_name'] = $v['real_name'];
            M('User')->save($v);
        }
    }

    public function sm(){
        $data = M('Community')->where(['children_count'=>array('gt',0)])->select();
        foreach ($data as $k=>$v) {
            $dd[$k][]= $v['name'];
            $dd[$k][]= $v['address'];
            $dd[$k][]= $v['full_parent_name'];
        }
//        myDump($data);
//        myDump($dd);die;
        $title = ['名字','地址','地区'];
        dumpExcel($title,$dd);
    }

    public function test5(){
        $this->display();
    }

    public function testJson(){
        $data = D('Service/OrderStep')->select();
        foreach ($data as $k=>&$v) {
            $v['data'] = str_replace('money','"money"',$v['data']);
            $v['data'] = str_replace('remark','"remark"',$v['data']);
            $v['data'] = str_replace('avatar','"avatar"',$v['data']);
            $v['data'] = str_replace('name','"name"',$v['data']);
            $v['data'] = str_replace('mobile','"mobile"',$v['data']);
            $v['data'] = str_replace('subtitle','"subtitle"',$v['data']);
            D('Service/OrderStep')->data($v)->save();
        }
    }

    private function getAccessToken(){
        $url = 'http://smart.wx91go.com/index.php?g=Home&m=Index&a=issubService&token=ofanap1448334221';
        $params['id'] = 22;
        $res = apiPost($url,$params);
        $res = json_decode($res);
        return $res;
    }

    public function getTag(){
        $accessToken = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token='.$accessToken;
        $res = Curl::curlPost($url,"get");
        $data = json_decode($res,true);
        myDump($data);
    }

    public function deleteTag(){
        $accessToken = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/tags/delete?access_token='.$accessToken;
        for ($x=245; $x<=282; $x++) {
//            $data=array();
//            $data['tag']['id'] = $x;
//            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
//            $res = Curl::curlPost($url,"post",$data);
        }
    }

    public function saveTagId(){
        $accessToken = $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token='.$accessToken;
        $res = Curl::curlPost($url,"get");
        $data = json_decode($res,true);
//        myDump($data);die;
        foreach ($data['tags'] as $v) {
            myDump($v['name']);
            M('Community')->data(['tag_id'=>$v['id']])->where(['name'=>array('like',$v['name'])])->save();
        }
    }

    public function repairDump(){
//        $depWhere['parentid'] = 14;
//        $depData = D('Admin/Depart')->select();
//        foreach ($depData as $v) {
//            $depId = $v['id'];
//        }
        $empWhere['id'] = array('gt',42);
        $data = D('Admin/Employee')->where($empWhere)->select();
//        myDump($data);
        foreach ($data as $k=>&$v) {
            $dep = explode(',',$v['department']);
            foreach ($dep as $vv) {
                $v['community_name'] .= D('Admin/Depart')->find($vv)['name'].',';
            }
        }
//        myDump($data);
        $title = ['姓名','手机号','微信号','职位','备注','小区'];
        foreach ($data as $k=>$v) {
            $dd[$k][] = $v['name'];
            $dd[$k][] = $v['mobile'];
            $dd[$k][] = $v['weixinid'];
            $dd[$k][] = $v['position']?$v['position']:'repair';
            $dd[$k][] = $v['remark'];
            $dd[$k][] = $v['community_name'];
        }
        dumpExcel($title,$dd,'维修');
    }
}
